package br.com.wss.recursos;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ManagerFile {
	private File file;
	private ObjectInputStream objInputStream;
	private ObjectOutputStream objOutputStream;

	public ManagerFile(File file) {
		this.file = file;
	}

	public String openFileWrite() {
		try {
			objOutputStream = new ObjectOutputStream(new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			return e.getMessage();
		} catch (IOException e) {
			return e.getMessage();
		}
		return "open";
	}

	public String openFileRead() {
		try {
			objInputStream = new ObjectInputStream(new FileInputStream(file));
		} catch (StreamCorruptedException e) {
			return e.getMessage();
		} catch (FileNotFoundException e) {
			return e.getMessage();
		} catch (IOException e) {
			return e.getMessage();
		}
		return "open";
	}

	public String writeFile(List<String> list) {
		try {
			objOutputStream.writeObject(list);
		} catch (IOException e) {
			return e.getMessage();
		}
		return "written";
	}

	public String writeFile(Map<String, ArrayList<String>> map) {
		openFileWrite();
		try {
			objOutputStream.writeObject(map);
		} catch (IOException e) {
			return e.getMessage();
		}finally{
			closedOut();
		}
		return "written";
	}
	/**
	 * 
	 * @return {@link List}
	 */
	public List<String> readFileList() {
		List<String> list = null;
		try {
			 list = (List<String>) objInputStream.readObject();
		} catch (OptionalDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 
	 * @return {@link Map}
	 */
	public Map<String, ArrayList<String>> readFileMap(File file) {
		Map<String, ArrayList<String>> map = null;
		openInput(file);
		try {
			 map =  (Map<String, ArrayList<String>>) objInputStream.readObject();
		} catch (OptionalDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		closedInput();
		return map;
	}
	
	private void openInput(File file) {
		try {
			objInputStream = new ObjectInputStream(new FileInputStream(file));
		} catch (StreamCorruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}

	public String closedOut() {
		if (objOutputStream != null) {
			try {
				objOutputStream.close();
			} catch (IOException e) {
				return e.toString();
			}
		}
		return "closed";
	}

	public String closedInput() {
		if (objInputStream != null) {
			try {
				objInputStream.close();
			} catch (IOException e) {
				return e.toString();
			}
		}
		return "closed";
	}
}
